package income

import (
	"encoding/json"
	"fmt"
	"time"
	"week13/errors"
	Repo "week13/internal/data/income"
	"week13/pkg/constant"
	"week13/pkg/orm"
)

var DefaultRepository Repo.IncomeRepository = &Repo.IncomeDao{}
type IncomeDTO struct {
	Id int64 `json:"id"`
	Amount int `json:"amount"`
	DealId int64 `json:"dealId"`
	AgentId int64 `json:"agentId"`
	Rate int `json:"rate"`
	SettleTime int `json:"settleTime"`
	State int `json:"state"`
	BizType int `json:"bizType"`
	Addtime int `json:"addtime"`
	Mtime string `json:"mtime"`
}

type Income struct {
	Id int64
	DTO *IncomeDTO
}
func NewIncomeWhere() Repo.IncomeWhere{
	return Repo.IncomeWhere{}
}
//根据 IncomeWhere 进行分页查询
func ListByWhere(con Repo.IncomeWhere,page int,pageSize int) ([]IncomeDTO,int64,error) {
	xorm := orm.NewXORMTransfer(con)
	ct, err := DefaultRepository.CountByWhere(xorm)
	if err != nil {
		return nil,0,err
	}
	if ct<1 {
		return []IncomeDTO{},0,nil
	}
	dos, err := DefaultRepository.ListByWhere(xorm,page,pageSize)
	if err != nil {
		return nil,0,err
	}
	dtos := make([]IncomeDTO,0)
	jsonData,_ := json.Marshal(dos)
	json.Unmarshal(jsonData,&dtos)
	return dtos,ct,nil
}
//创建收入
func CreateIncome(dto IncomeDTO) (*Income,error){
	if dto.Id>1 || dto.Amount<1 || dto.BizType <0 || dto.AgentId<1{
		return nil, errors.NewIllegalParam("create deal params error")
	}
	dto.Id = 0
	do := DTOToDo(dto)
	do.State = 1
	ret,err := DefaultRepository.Save(do)
	if err != nil {
		return nil, errors.NewExecuteFail(err.Error())
	}
	return NewIncome(ret)
}
//找到一条收入记录
func FindOne(dto IncomeDTO) (IncomeDTO,error) {
	filter := DTOToDo(dto)
	do, err := DefaultRepository.FindOneByFilter(filter)
	if err != nil {
		return IncomeDTO{},errors.NewExecuteFail(err.Error())
	}
	dto = DoToDTO(do)
	return dto,nil
}
//根据DTO 进行分页查找
func List(dto IncomeDTO,page int,pageSize int) ([]IncomeDTO,int64,error) {
	filter := DTOToDo(dto)
	ct, err := DefaultRepository.Count(filter)
	if err !=nil {
		return nil,0,err
	}
	if ct <1 {
		return nil,0,nil
	}
	dos, err := DefaultRepository.List(filter,page,pageSize)
	fmt.Println("dto-----",dos)
	if err != nil {
		return nil,0,err
	}
	dtos := make([]IncomeDTO,0)
	jsonData,_ := json.Marshal(dos)
	json.Unmarshal(jsonData,&dtos)
	return dtos,ct,nil
}
//实例化收入
func NewIncome (Id int64) (*Income,error){
	income := &Income{Id: Id}
	do, err := DefaultRepository.Find(income.Id)
	if err != nil {
		return nil,errors.NewExecuteFail(err.Error())
	}
	dto :=DoToDTO(do)
	income.DTO = &dto
	return income,nil
}
//计算结算时间
func CalSettleTime(itemId int) int {
	day := 5 + itemId%10
	return int(time.Now().Unix())+(day*86400)
}
//设置收入状态
func (income *Income) SetState(state int) (int64,error){
	if state != constant.INCOME_STATE_INVALID || state != constant.INCOME_STATE_SETTLE {
		return 0, errors.NewIllegalParam("Set Income,State error")
	}
	data, err := DefaultRepository.Find(income.Id)
	if err != nil {
		return 0, err
	}
	if data.State == constant.INCOME_STATE_SETTLE {
		return 0, errors.NewIllegalParam("Set Income,State has settle,can't change")
	}
	now := int(time.Now().Unix())
	do := Repo.IncomeDo{State: state}
	if data.SettleTime < now && state == constant.INCOME_STATE_INIT {
		do.SettleTime = now + 86400
	}
	return income.save(do)
}
func (income *Income) save(do Repo.IncomeDo)(int64,error){
	do.DealId = income.Id
	ret,err := DefaultRepository.Save(do)
	return ret,err
}
func DTOToDo(dto IncomeDTO) Repo.IncomeDo{
	jsonData,_ := json.Marshal(dto)
	do := Repo.IncomeDo{}
	json.Unmarshal(jsonData,&do)
	return do
}
func DoToDTO(do Repo.IncomeDo) IncomeDTO {
	jsonData,_ := json.Marshal(do)
	dto := IncomeDTO{}
	json.Unmarshal(jsonData,&dto)
	return dto
}